﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using System.Text;


using log4net;
using log4net.Config;
using System.Reflection;


namespace AutoRule
{
    class CRegMainWorker
    {
        public void Start()
        {
            _worker.Start();
        }

        public void Stop()
        {
            _WorkerStopEvent.Set();
            _worker.Join();
         }

        public static void dowork()
        {
            while (true)
            {
                CRegRuleManager.GetInstance().Parse();
                List<CRegRule> RuleList = CRegRuleManager.GetInstance().Get();
                if (RuleList.Count < 1)
                    return;

                String Url = "http://" + CConfig.GetInstance()._OpcSvrIp + ":" + CConfig.GetInstance()._OpcSvrPort + "/Data";
                String request = "action=getvars&";
                foreach (CRegRule it in RuleList)
                {
                    request += it.varid + "|";
                }
                request = request.Substring(0, request.Length - 1);
                CHttpClient client = new CHttpClient();
                String reponse = client.HttpPost(Url, request);
                
                ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
                log.Info(reponse);
                String[] sReq = reponse.Split('&');
                if (sReq.Length == 2)
                {
                    String[] vars = sReq[1].Split(',');
                    foreach (String it in vars)
                    {
                        String[] values = it.Split('=');
                        if (values[1].Equals("null"))
                        {
                            
                            log.Warn("id:" + values[0] + " value: null");
                            continue;
                        }
                        foreach (CRegRule itRule in RuleList)
                        {
                            if (itRule.Hours == 0)
                                continue;
                            if (itRule.varid == int.Parse(values[0]))
                            {
                                if (int.Parse(values[1]) % itRule.Hours == 0)
                                {
                                    CDB.GetInstance().InsertRegMainRuleReport(itRule.varid, itRule.DevName, int.Parse(values[1]));
                                }
                                break;
                            }
                        }
                    }
                }
                else
                {

                }

                int nWait = 60 * 60 * 1000 - DateTime.Now.Minute * 60 * 1000 - DateTime.Now.Second * 1000 - DateTime.Now.Millisecond + 27 * 1000;
                if (_WorkerStopEvent.WaitOne(nWait, true))
                    return;
            }
            
        }







       private static ManualResetEvent _WorkerStopEvent = new ManualResetEvent(false);
       private Thread _worker = new Thread(new ThreadStart(dowork));
    }

 



}
